using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

//[DefaultExecutionOrder(2)]
public class DragInventory : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    public InventoryController inventoryController;
    public ItemGrid selectedItemGrid;
    public InventoryItem selectedItem;

    private void Awake()
    {
        GameObject tarObject = GameObject.Find("inventorycontroller");
        inventoryController = tarObject.GetComponent<InventoryController>();
        selectedItemGrid = inventoryController.SelectedItemGrid;
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        selectedItem = GetComponent<InventoryItem>();
        transform.position = eventData.position;
        selectedItemGrid = inventoryController.SelectedItemGrid;
        inventoryController.selectedItem = selectedItem;
        PickUpItem();


    }

    public void OnDrag(PointerEventData eventData)
    {
        transform.position = eventData.position;
        selectedItemGrid = inventoryController.SelectedItemGrid;
        if (selectedItemGrid != null)
        {
            selectedItem.setParent(selectedItemGrid);
            Vector2Int tileGridPosition = GetTileGridPosition(eventData.position);
            if (selectedItemGrid.isOutSize(tileGridPosition.x, tileGridPosition.y, selectedItem.itemData.sizeWidth, selectedItem.itemData.sizeHeight))
            {
                return;
            }
            bool noRotate = selectedItemGrid.CheckAvailableSpace(tileGridPosition.x, tileGridPosition.y, selectedItem.itemData.sizeWidth, selectedItem.itemData.sizeHeight);
            bool Rotate = selectedItemGrid.CheckAvailableSpace(tileGridPosition.x, tileGridPosition.y, selectedItem.itemData.sizeHeight, selectedItem.itemData.sizeWidth);
            if (noRotate)
            {
                selectedItem.Rotate(false);
                return;
            }
            if (Rotate)
            {
                selectedItem.Rotate(true);
                return;
            }


            if (!noRotate && !Rotate)
            {
                selectedItem.Rotate(false);
                return;
            }
           

        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        if (selectedItemGrid == null) {
            selectedItem.lastItemGrid.placeItem(selectedItem, selectedItem.lastItemPosition.x, selectedItem.lastItemPosition.y);
            inventoryController.selectedItem = null;
            return;
        }
        if (selectedItemGrid.ItemDataType == ItemDataType.normal)
        {
            transform.position = eventData.position;
            Vector2Int tileGridPosition = GetTileGridPosition(eventData.position);
            Boolean isBound = selectedItemGrid.BoundryCheck(tileGridPosition.x, tileGridPosition.y, selectedItem.WIDTH, selectedItem.HEIGHT);

            if (!isBound)
            {
                selectedItem.lastItemGrid.placeLastItem(selectedItem, selectedItem.lastItemPosition.x, selectedItem.lastItemPosition.y);
                
                inventoryController.selectedItem = null;
                return;
            }

            //bool iscanPlace = selectedItemGrid.judgeCanPlace(tileGridPosition.x, tileGridPosition.y, selectedItem);
            bool iscanPlace = selectedItemGrid.judgeCanPlaceNew(tileGridPosition.x, tileGridPosition.y, selectedItem);

            if (!iscanPlace)
            {
                selectedItem.lastItemGrid.placeLastItem(selectedItem, selectedItem.lastItemPosition.x, selectedItem.lastItemPosition.y);
                inventoryController.selectedItem = null;
                return;
            }
            List<ToSwapInventoryItem> swapInventoryItems = selectedItemGrid.getToSwapInventoryItemsAndClean(tileGridPosition.x, tileGridPosition.y, selectedItem, true);
            Vector2Int lastPosition = selectedItem.lastItemPosition;
            ItemGrid lastGrid = selectedItem.lastItemGrid;
            selectedItemGrid.placeItem(selectedItem, tileGridPosition.x, tileGridPosition.y);

            //lastGrid.handleSwap(lastPosition, swapInventoryItems, lastGrid);
            lastGrid.handleSwapNew(lastPosition, swapInventoryItems, lastGrid);
        }
        else {
            bool canPlace = selectedItemGrid.judgeCanPlaceOther(selectedItem);
            if (!canPlace)
            {
                selectedItem.lastItemGrid.placeLastItem(selectedItem, selectedItem.lastItemPosition.x, selectedItem.lastItemPosition.y);
                return;
            }
            selectedItemGrid.placeOtherItem(selectedItem);
            
        }
        
        
        inventoryController.selectedItem = null;
    }



    private Vector2Int GetTileGridPosition(Vector2 productPosition)
    {
        UnityEngine.Vector2 position = productPosition;
        position.x -= (selectedItem.WIDTH - 1) * selectedItemGrid.tileSizeWidth / 2;
        position.y += (selectedItem.HEIGHT - 1) * selectedItemGrid.tileSizeHeight / 2;

        return selectedItemGrid.GetTileGridPosition(position);
    }


    private void PickUpItem()
    {
        if (selectedItem != null)
        {
            if (selectedItem.lastItemGrid.ItemDataType == ItemDataType.normal)
            {
                bool isOk = selectedItem.lastItemGrid.pickUpItemHandle(selectedItem);
            }
            else {
                bool isOk = selectedItem.lastItemGrid.pickUpOtherItemHandle(selectedItem);
                selectedItem.set(selectedItem.itemData,20,20);
            }
            
            //if (!isOk)
            //{
            //    selectedItemGrid.placeItem(selectedItem, selectedItem.lastItemPosition.x, selectedItem.lastItemPosition.y);
            //}
        }
    }
}
